﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VipSys.DataLayer;
using VipSys.SystemEntity;
using VipSys.Common.Library;
using VipSys.Business;
namespace VipSys
{
    public partial class UserMag : System.Web.UI.Page
    {
        private List<User> m_lstUser;
        protected void Page_Load(object sender, EventArgs e)
        {
            SysAccount sysAccount = Session["SysAccount"] as SysAccount;
            if (sysAccount==null)
            {
                this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('登录超时，请重新登录!')</script>");
                return;
            }
            string action = Request["action"];
            if (!string.IsNullOrEmpty(action))
            {
                switch (action)
                {
                    case "BindGrid":
                        BindGrid();
                        return;
                    case "GetOrgnization":
                        GetOrgnization();
                        return;
                    case "GetEmployee":
                        GetEmployee();
                        return;
                    case "BindRoleType":
                        BindRoleType();
                        return;
                    case "BindTemplate":
                        BindTemplate();
                        return;
                    case "Add":
                        Add();
                        return;
                    case "Edit":
                        Edit();
                        return;
                    case "Del":
                        Del();
                        return;
                    default:
                        return;
                }
            }
        }

        private void Add()
        {

            SysAccount sysAccount = Session["SysAccount"] as SysAccount;
            string jsons = "";
            User user = new User();
            var Sequence = Request["Sequence"];
            var Name = Request["Name"];
            var Gender = Request["Gender"];
            var Tel = Request["Tel"];
            var School = Request["School"];
            var PayPalce = Request["PayPlace"];
            var Template = Request["Template"];
            var ExamSchool = Request["ExamSchool"];
            var ExamPlace = Request["ExamPlace"];
            var ExceptRoomie = Request["ExceptRoomie"];
            var Bak = Request["Bak"];
            var PayMoney = Request["PayMoney"];
            //var EmployeeID = Request["EmployeeID"];
            user.Sequence = Sequence;
            user.Name = Name;
            user.Gender = Gender;
            user.Tel = Tel;
            user.School = School;
            user.PayPlace = PayPalce;
            user.Template = Template;
            user.ExamSchool = ExamSchool;
            user.ExamPlace = ExamPlace;
            user.ExceptRoomie = ExceptRoomie;
            user.Bak = Bak;
            user.PayMoney = PayMoney;
            user.EmployeeID = sysAccount.ID.ToString();
            user.PassWord = AccountCreat.Instance.GetPwd(Tel);
            short shRet = UserAccess.Instance.Add(user);
            if (shRet != SystemData.ReturnValue.OK)
            {
                jsons = "{\"success\":false,msg:\"新增失败\"}";
            }
            else
            {
                jsons = "{\"success\":true,msg:\"新增成功\"}";
            }
            this.Response.Clear();
            this.Response.Write(jsons);
            this.Response.End();
        }

        private void Edit()
        {
            string jsons = "";
            User user = new User();

            var ID = Request["ID"];
            var Sequence = Request["Sequence"];
            var Name = Request["Name"];
            var Gender = Request["Gender"];
            var Tel = Request["Tel"];
            var School = Request["School"];
            var PayPlace = Request["PayPlace"];
            var Template = Request["Template"];
            var ExamSchool = Request["ExamSchool"];
            var ExamPlace = Request["ExamPlace"];
            var ExceptRoomie = Request["ExceptRoomie"];
            var Bak = Request["Bak"];
            var PayMoney = Request["PayMoney"];
           // var EmployeeID = Request["EmployeeID"];
           
            user.ID = int.Parse(ID);
            UserAccess.Instance.GetUser(user, ref user);
            user.Sequence = Sequence;
            user.Name = Name;
            user.Gender = Gender;
            user.Tel = Tel;
            user.School = School;
            user.PayPlace = PayPlace;
            user.Template = Template;
            user.ExamSchool = ExamSchool;
            user.ExamPlace = ExamPlace;
            user.ExceptRoomie = ExceptRoomie;
            user.Bak = Bak;
            user.PayMoney = PayMoney;
           // user.EmployeeID = EmployeeID;
            short shRet = UserAccess.Instance.Edit(user);
            if (shRet != SystemData.ReturnValue.OK)
            {
                jsons = "{\"success\":false,msg:\"编辑失败\"}";
            }
            else
            {
                jsons = "{\"success\":true,msg:\"编辑成功\"}";
            }
            this.Response.Clear();
            this.Response.Write(jsons);
            this.Response.End();
        }
        private void Del()
        {
            string jsons = "";
            User user = new User();
            var deletesplit = Request["deletesplit"];
            foreach (var ID in deletesplit.Split(','))
            {
                user.ID = int.Parse(ID);
                short shRet = UserAccess.Instance.Del(user);
                if (shRet != SystemData.ReturnValue.OK)
                {
                    jsons = "{\"success\":true,msg:\"删除失败\"}";
                    break;
                }
            }
            if (string.IsNullOrEmpty(jsons))
                jsons = "{\"success\":true,msg:\"删除成功\"}";
            this.Response.Clear();
            this.Response.Write(jsons);
            this.Response.End();
        }
        private void BindGrid()
        {
            SysAccount sysAccount = Session["SysAccount"] as SysAccount;
            JSONHelper json = new JSONHelper();
            json.success = true;
            string jsons = "";
            User query = new User();
            int start= int.Parse(Request.Form["start"]);
            int limit = int.Parse(Request.Form["limit"]);
            string szName = Request.Form["Name"];
            string szEmployeeID = Request.Form["EmployeeID"];
            if (!string.IsNullOrEmpty(sysAccount.Name))
            {
                if (query.Start != null && query.Limit != null)
                {
                    json.success = true;
                    string szSchools = ViewAuthority.Instance.GetSchools(sysAccount.RoleType,sysAccount.OrgName);
                    if (sysAccount.RoleType == SystemData.RoleType.SCHOOL)
                    {
                        szEmployeeID = sysAccount.ID.ToString();
                        szSchools = "";
                    }

                    short shRet = UserAccess.Instance.GetUserPageList(szName, szEmployeeID,sysAccount.RoleType, start, limit, ref this.m_lstUser, szSchools);

                    foreach (User item in this.m_lstUser)
                    {
                        json.AddItem("ID", item.ID.ToString());
                        json.AddItem("Bak", item.Bak);
                        json.AddItem("ExamPlace", item.ExamPlace);
                        json.AddItem("EmployeeID", item.EmployeeID);
                        json.AddItem("EmployeeName", item.EmployeeName);
                        json.AddItem("ExamSchool", item.ExamSchool);
                        json.AddItem("Gender", item.Gender);
                        json.AddItem("Hotel", item.Hotel);
                        json.AddItem("HotelExpense", item.HotelExpense);
                        json.AddItem("MoneyBack", item.MoneyBack);
                        json.AddItem("Name", item.Name);
                        json.AddItem("PassWord", item.PassWord);
                        json.AddItem("PayMoney", item.PayMoney);
                        json.AddItem("Room", item.Room);
                        json.AddItem("School", item.School);
                        json.AddItem("Sequence", item.Sequence);
                        json.AddItem("Tel", item.Tel);
                        json.AddItem("EmployeeID", item.EmployeeID);
                        json.AddItem("EmployeeName", item.EmployeeName);
                        json.AddItem("ExceptRoomie", item.ExceptRoomie);
                        json.AddItem("Template", item.Template);
                        json.AddItem("PayPlace", item.PayPlace);
                        json.ItemOk();
                    }
                    int totalCount = 0;
                    shRet = UserAccess.Instance.GetTotalCount(szName, szEmployeeID,sysAccount.ID.ToString(), ref totalCount, szSchools);
                    json.totlalCount = totalCount;
                    if (json.totlalCount > 0)
                    {
                        jsons = json.ToString();
                    }
                    else
                    {
                        jsons = @"[]";
                    }
                }
                else
                {
                    jsons = @"{success:false}";
                }
            }
            else
            {
                jsons = @"{success:false}";
            }


            this.Response.Clear();
            this.Response.Write(jsons);
            this.Response.End();
        }
        private void GetOrgnization()
        {
            JSONHelper json = new JSONHelper();
            json.success = true;
            string jsons = "";
            Orgnization query = new Orgnization();
            List<Orgnization> lstOrgnization = new List<Orgnization>();
            query.Start = Request.Form["start"];
            query.Limit = Request.Form["limit"];
            string OrgName = Request.Form["OrgName"];
            string RoleType = Request.Form["RoleType"];
            query.OrgName = OrgName;
            query.RoleType = RoleType;
            if (query.Start != null && query.Limit != null)
            {
                json.success = true;
                short shRet = OrgnizationAccess.Instance.GetOrgnizationPageList(query, ref lstOrgnization);

                foreach (Orgnization item in lstOrgnization)
                {
                    json.AddItem("ID", item.ID.ToString());
                    json.AddItem("OrgName", item.OrgName);
                    json.AddItem("RoleType", item.RoleType);
                    json.ItemOk();
                }
                int totalCount = 0;
                shRet = OrgnizationAccess.Instance.GetTotalCount(query, ref totalCount);
                json.totlalCount = totalCount;
                if (json.totlalCount > 0)
                {
                    jsons = json.ToString();
                }
                else
                {
                    jsons = @"[]";
                }
            }
            else
            {
                jsons = @"{success:false}";
            }

            this.Response.Clear();
            this.Response.Write(jsons);
            this.Response.End();
        }
        private void GetEmployee()
        {
            JSONHelper json = new JSONHelper();
            json.success = true;
            string jsons = "";
            Employee query = new Employee();
            List<Employee> lstEmployee = new List<Employee>();
            query.Start = Request.Form["start"];
            query.Limit = Request.Form["limit"];
            string OrgName = Request.Form["OrgName"];
            string RoleType = Request.Form["RoleType"];
         
            if (query.Start != null && query.Limit != null)
            {
                json.success = true;
                short shRet = EmployeeAccess.Instance.GetEmployeePageList(query, ref lstEmployee);

                foreach (Employee item in lstEmployee)
                {
                    json.AddItem("ID", item.ID.ToString());
                    json.AddItem("OrgName", item.OrgName);
                    json.AddItem("EmpNo", item.EmpNo);
                    json.AddItem("OrgName", item.OrgName);
                    json.AddItem("PassWord", item.PassWord);
                    json.AddItem("RoleType", item.RoleType);
                    json.AddItem("Name", item.Name);
                    json.AddItem("Tel", item.Tel);
                    json.ItemOk();
                }
                int totalCount = 0;
                shRet = EmployeeAccess.Instance.GetTotalCount(query, ref totalCount);
                json.totlalCount = totalCount;
                if (json.totlalCount > 0)
                {
                    jsons = json.ToString();
                }
                else
                {
                    jsons = @"[]";
                }
            }
            else
            {
                jsons = @"{success:false}";
            }

            this.Response.Clear();
            this.Response.Write(jsons);
            this.Response.End();
        }
        public void BindRoleType()
        {
            JSONHelper json = new JSONHelper();
            json.success = true;
            string jsons = "";
            json.success = true;
            List<SysCombo> lstRoleType = new List<SysCombo>();
            ComboAccess.Instance.GetRoleTypes(ref lstRoleType);
            foreach (var row in lstRoleType)
            {
                json.AddItem("value", row.Value);
                json.AddItem("name", row.Name);
                json.ItemOk();
            }
            json.totlalCount = lstRoleType.Count();
            if (json.totlalCount > 0)
            {
                jsons = json.ToString();
            }
            else
            {
                jsons = @"[]";
            }
            this.Response.Clear();
            this.Response.Write(jsons);
            this.Response.End();
        }
        public void BindTemplate()
        {
            JSONHelper json = new JSONHelper();
            json.success = true;
            string jsons = "";
            json.success = true;
            List<SysCombo> lstTemplate = new List<SysCombo>();
            ComboAccess.Instance.GetTemplates(ref lstTemplate);
            foreach (var row in lstTemplate)
            {
                json.AddItem("value", row.Name);
                json.AddItem("name", row.Name);
                json.ItemOk();
            }
            json.totlalCount = lstTemplate.Count();
            if (json.totlalCount > 0)
            {
                jsons = json.ToString();
            }
            else
            {
                jsons = @"[]";
            }
            this.Response.Clear();
            this.Response.Write(jsons);
            this.Response.End();
        }
    }
}